<%-- 
    Document   : FCompany
    Created on : 01/05/2010, 10:08:22
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="br.terminalrodoviario.Company"%>
<%@page import="br.terminalrodoviario.City"%>
<%@page import="java.util.*, java.text.*" %>
<jsp:useBean class="br.terminalrodoviario.Company" id="Company" scope="page"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%
            String id = request.getParameter("id");
            if (id != null) {
                Company = new Company().selectById("Select c from Company c where c.id = " + id);
            }
%>

<jsp:useBean class="br.terminalrodoviario.User" id="user" scope="session"/>
<%
        if (user.getId() == 0) {
            response.sendRedirect("../login.jsp");
        }
%>

<script type="text/javascript">
    function validaCNPJ(cnpj) {
        var i = 0;
        var l = 0;
        var strNum = "";
        var strMul = "6543298765432";
        var character = "";
        var iValido = 1;
        var iSoma = 0;
        var strNum_base = "";
        var iLenNum_base = 0;
        var iLenMul = 0;
        var iSoma = 0;
        var strNum_base = 0;
        var iLenNum_base = 0;

        if (cnpj == "")
            return ("Informe o CNPJ");

        l = cnpj.length;
        for (i = 0; i < l; i++) {
            caracter = cnpj.substring(i,i+1)
            if ((caracter >= '0') && (caracter <= '9'))
                strNum = strNum + caracter;
        };

        if(strNum.length != 14)
            return ("Informe 14 caracteres");

        strNum_base = strNum.substring(0,12);
        iLenNum_base = strNum_base.length - 1;
        iLenMul = strMul.length - 1;
        for(i = 0;i < 12; i++)
            iSoma = iSoma +
            parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) *
            parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10);

        iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11);
        if(iSoma == 11 || iSoma == 10)
            iSoma = 0;

        strNum_base = strNum_base + iSoma;
        iSoma = 0;
        iLenNum_base = strNum_base.length - 1
        for(i = 0; i < 13; i++)
            iSoma = iSoma +
            parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) *
            parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10)

        iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11);
        if(iSoma == 11 || iSoma == 10)
            iSoma = 0;
        strNum_base = strNum_base + iSoma;
        if(strNum != strNum_base)
            return ("CNPJ inválido");

        return (true);
    }
    function verifyFields(){
        var txtName = document.getElementById("txtName");
        var txtLogin = document.getElementById("txtLogin");
        var txtSenha = document.getElementById("txtPassword");
        var cboState = document.getElementById("cboState");
        var cboCity = document.getElementById("cboCities");
        var txtDate = document.getElementById("txtDateFundation");
        var txtCNPJ = document.getElementById("txtCNPJ");
        document.getElementById('validate').innerHTML = '';
        document.getElementById('validate1').innerHTML = '';
        document.getElementById('validate2').innerHTML = '';
        document.getElementById('validate3').innerHTML = '';
        document.getElementById('validate4').innerHTML = '';
        document.getElementById('validate5').innerHTML = '';
        document.getElementById('validate6').innerHTML = '';
        var fill = true;

        var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
        if (!((txtDate.value.match(expReg)) && (txtDate.value!=''))) {
            document.getElementById('validate5').innerHTML = 'Formato inválido de data';
            fill=false;
            txtDate.focus();
        }

        if(txtName.value==""){
            document.getElementById('validate').innerHTML = 'Informe o nome';
            fill=false;
        }
        if(txtLogin.value==""){
            document.getElementById('validate1').innerHTML = 'Informe o login';
            fill=false;
        }
        if(txtSenha.value==""){
            document.getElementById('validate2').innerHTML = 'Informe a senha';
            fill=false;
        }
        if(txtCNPJ.value==""){
            document.getElementById('validate6').innerHTML = 'Informe o CNPJ';
            fill=false;
        }
        /*if(cboState.value=="" || cboState.value==null){
            document.getElementById('validate3').innerHTML = 'Selecione o estado';
            fill=false;
        }
        if(cboCity.value=="" || cboCity.value==null){
            document.getElementById('validate4').innerHTML = 'Selecione a cidade';
            fill=false;
        }*/
        if(fill==false)
            return;
        document.getElementById("form").submit();
    }
</script>

<html>
    <head>        
        <title>Guia Terminal Rodoviário</title>
        <%SimpleDateFormat formatBra = new SimpleDateFormat("dd/MM/yyyy");%>
        <script type="text/javascript" src="../../add/js/cities.js"></script>
        <style type="text/css">
            @import url("../../add/css/cssAdmin.css");
            @import url("../../add/css/rodape.css");
        </style>
    </head>
    <body>
        <div class="geral">
            <table>
                <tr>
                    <td>
                        <div id="cabecalho">
                        </div>
                    </td>
                </tr>
            </table>
            <table id="menuAdmin" width="762">
                <tr >
                    <td>
                        <ul class="menuCidade" >
                            <li><a href="../Manager/MCity.jsp" /></li>
                        </ul>
                    </td>
                    <td>
                        <ul class="menuEmpresa" >
                            <li class="current"><a href="../Manager/MCompany.jsp" /></li>
                        </ul>
                    </td>
                    <td>
                        <ul class="menuTerminais" >
                            <li><a href="../Manager/MBusStation.jsp" /></li>
                        </ul>
                    </td>
                    <td>
                        <ul class="menuHoteis" >
                            <li><a href="../Manager/MHotel.jsp" /></li>
                        </ul>
                    </td>
                    <td>
                        <ul class="menuNoticias" >
                            <li><a href="../Manager/MNotice.jsp" /></li>
                        </ul>
                    </td>
                    <td>
                        <ul class="menuSolicitacao" >
                            <li><a href="../Manager/MSolicitation.jsp" /></li>
                        </ul>
                    </td>
                    <td>
                        <ul class="menuDicas" >
                            <li><a href="../Manager/MTravelTip.jsp" /></li>
                        </ul>
                    </td>

                </tr>
            </table>

            <div style="float:left">
                <br/>
                <b><a href="../index.jsp">Início</a> -> <a href="../Manager/MCompany.jsp">Empresas</a> -> <a href="FCompany.jsp">Cadastrar Empresa</a></b>
                <br/><br/>
            </div>
            <div align="right" style="font-family:calibri; font-size:11px; font-style:inherit">
                <br/>Bem-vindo <b><%=request.getSession().getAttribute("username")%></b>,<br/>
                você está logado desde <%=request.getSession().getAttribute("date_session")%>h. ( <a href="../logout.jsp"><b>Logout</b></a> )<br/><br/>
            </div>

            <table  id="subTitulo">
                <tr>
                    <td id="icone">
                        &nbsp;&nbsp;&nbsp;&nbsp;<img src="../../add/images/icones/company.png" alt="Empresas" title="Empresas"/>
                    </td>
                    <td>
                        Empresas
                    </td>
                </tr>
            </table>

            <form id="form" action="../CompanyServlet?acao=gravar&id=<%=request.getParameter("id")%>" method="post" enctype="multipart/form-data">                
                <div align="right"><b style="color:red">(*) campos obrigatórios</b></div>
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <td class="label_bg" style="width:150px;">&nbsp;&nbsp;Nome Fantasia:</td>
                        <td >&nbsp;&nbsp;<input type="text" id="txtName" name="txtName" value="<%=Company.getName() == null ? "" : Company.getName()%>" size="70"> <b style="color:red">*</b> <b id="validate"></b></td>
                    </tr>
                    <tr>
                        <td class="label"> &nbsp;&nbsp;Login:</td>
                        <td >&nbsp;&nbsp;<input type="text" id="txtLogin" name="txtLogin" value="<%=Company.getLogin() == null ? "" : Company.getLogin()%>" size="70"> <b style="color:red">*</b> <b id="validate1"></b></td>
                    </tr>
                    <tr>
                        <td class="label_bg" style="width:150px;">&nbsp;&nbsp;Senha:</td>
                        <td >&nbsp;&nbsp;<input type="password" id="txtPassword" name="txtPassword" value="<%=Company.getPassword() == null ? "" : Company.getPassword()%>" size="70"> <b style="color:red">*</b> <b id="validate2"></b></td>
                    </tr>
                    <tr>
                        <td class="label">&nbsp;&nbsp;CNPJ:</td>
                        <td >&nbsp;&nbsp;<input type="text" id="txtCNPJ" name="txtCNPJ" maxlength="14" onblur="document.getElementById('validate6').innerHTML = (validaCNPJ(this.value)==true?'':validaCNPJ(this.value));" value="<%=Company.getCnpj() == null ? "" : Company.getCnpj()%>" size="70"> <b style="color:red">*</b> <b id="validate6"></b></td>
                    </tr>
                    <tr>
                        <td class="label_bg">&nbsp;&nbsp;Endereço:</td>
                        <td >&nbsp;&nbsp;<input type="text" name="txtAddress" value="<%=Company.getAddress() == null ? "" : Company.getAddress()%>" size="70"></td>
                    </tr>
                    <tr>
                        <td class="label" style="width:150px;">&nbsp;&nbsp;Inscrição Estadual:</td>
                        <td >&nbsp;&nbsp;<input type="text" name="txtStateRegistration" value="<%=Company.getStateRegistration() == null ? "" : Company.getStateRegistration()%>" size="70"></td>
                    </tr>
                    <tr>
                        <td class="label_bg">&nbsp;&nbsp;Inscrição Municipal:</td>
                        <td >&nbsp;&nbsp;<input type="text" name="txtMunicipalRegistration" value="<%=Company.getMunicipalRegistration() == null ? "" : Company.getMunicipalRegistration()%>" size="70"></td>
                    </tr>
                    <tr>
                        <td class="label" style="width:150px;">&nbsp;&nbsp;Razão Social:</td>
                        <td >&nbsp;&nbsp;<input type="text" name="txtCorporateName" value="<%=Company.getCorporateName() == null ? "" : Company.getCorporateName()%>" size="70"></td>
                    </tr>
                    <tr>
                        <td class="label_bg">&nbsp;&nbsp;CEP:</td>
                        <td >&nbsp;&nbsp;<input type="text" name="txtCEP" value="<%=Company.getCep() == null ? "" : Company.getCep()%>" size="70"></td>
                    </tr>
                    <tr>
                        <td class="label" style="width:150px;">&nbsp;&nbsp;Telefone:</td>
                        <td>&nbsp;&nbsp;<input type="text" name="txtPhone" value="<%=Company.getPhone() == null ? "" : Company.getPhone()%>" size="70"></td>
                    </tr>
                    <tr>
                        <td class="label_bg">&nbsp;&nbsp;Fax:</td>
                        <td>&nbsp;&nbsp;<input type="text" name="txtFax" value="<%=Company.getFax() == null ? "" : Company.getFax()%>" size="70"></td>
                    </tr>
                    <tr>
                        <td class="label" style="width:150px;">&nbsp;&nbsp;Website:</td>
                        <td>&nbsp;&nbsp;<input type="text" name="txtWebsite" value="<%=Company.getWebSite() == null ? "" : Company.getWebSite()%>" size="70"></td>
                    </tr>
                    <tr>
                        <td class="label_bg">&nbsp;&nbsp;E-mail:</td>
                        <td>&nbsp;&nbsp;<input type="text" name="txtEmail" value="<%=Company.getEmail() == null ? "" : Company.getEmail()%>" size="70"></td>
                    </tr>
                    <tr>
                        <td class="label" style="width:150px;">&nbsp;&nbsp;Data de Fundação:</td>
                        <td>&nbsp;&nbsp;<input type="text" id="txtDateFundation" name="txtDateFundation" value="<%=Company.getDateFundation() == null ? formatBra.format(new java.util.Date()) : formatBra.format(Company.getDateFundation())%>" style="width:245px;"> <b id="validate5"></b></td>
                    </tr>
                    <tr>
                        <td class="label_bg">&nbsp;&nbsp;Estado:</td>
                        <td>&nbsp;
                            <select class="calibri" name="cboState" style="width:250px; height:22px;" onchange="javascript:findCities(this.options[this.selectedIndex].value,'../../StateCityServlet','cboCities');">
                                <option value="AC" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("AC") ? "selected='true'" : ""%>>Acre</option>
                                <option value="AL" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("AL") ? "selected='true'" : ""%>>Alagoas</option>
                                <option value="AM" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("AM") ? "selected='true'" : ""%>>Amazonas</option>
                                <option value="AP" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("AP") ? "selected='true'" : ""%>>Amapá</option>
                                <option value="BA" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("BA") ? "selected='true'" : ""%>>Bahia</option>
                                <option value="CE" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("CE") ? "selected='true'" : ""%>>Ceará</option>
                                <option value="DF" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("DF") ? "selected='true'" : ""%>>Distrito Federal</option>
                                <option value="ES" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("ES") ? "selected='true'" : ""%>>Espírito Santo</option>
                                <option value="GO" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("GO") ? "selected='true'" : ""%>>Goiás</option>
                                <option value="MA" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("MA") ? "selected='true'" : ""%>>Maranhão</option>
                                <option value="MT" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("MT") ? "selected='true'" : ""%>>Mato Grosso</option>
                                <option value="MS" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("MS") ? "selected='true'" : ""%>>Mato Grosso do Sul</option>
                                <option value="MG" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("MG") ? "selected='true'" : ""%>>Minas Gerais</option>
                                <option value="PA" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("PA") ? "selected='true'" : ""%>>Pará</option>
                                <option value="PB" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("PB") ? "selected='true'" : ""%>>Paraíba</option>
                                <option value="PR" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("PR") ? "selected='true'" : ""%>>Paraná</option>
                                <option value="PE" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("PE") ? "selected='true'" : ""%>>Pernambuco</option>
                                <option value="PI" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("PI") ? "selected='true'" : ""%>>Piauí</option>
                                <option value="RJ" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("RJ") ? "selected='true'" : ""%>>Rio de Janeiro</option>
                                <option value="RN" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("RN") ? "selected='true'" : ""%>>Rio Grande do Norte</option>
                                <option value="RO" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("RO") ? "selected='true'" : ""%>>Rondônia</option>
                                <option value="RS" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("RS") ? "selected='true'" : ""%>>Rio Grande do Sul</option>
                                <option value="RR" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("RR") ? "selected='true'" : ""%>>Roraima</option>
                                <option value="SC" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("SC") ? "selected='true'" : ""%>>Santa Catarina</option>
                                <option value="SE" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("SE") ? "selected='true'" : ""%>>Sergipe</option>
                                <option value="SP" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("SP") ? "selected='true'" : ""%>>São Paulo</option>
                                <option value="TO" <%=Company.getCity() != null && Company.getCity().getState() != null && Company.getCity().getState().equals("TO") ? "selected='true'" : ""%>>Tocantins</option>
                            </select>
                            <b style="color:red">*</b> <b id="validate3"></b>
                        </td>
                    </tr>
                    <tr>
                        <td class="label" style="width:150px;">&nbsp;&nbsp;Cidade:</td>
                        <td>&nbsp;
                            <select class="calibri" name="cboCities" id="cboCities" style="width:250px; height:22px;">
                                <%
                                            int i = 0;
                                            for (City c : new City().select("select c from City c where c.removed = 0 and state = '" + (Company.getCity() != null ? Company.getCity().getState() : "AC") + "' or c.id = " + (Company.getCity() != null ? Company.getCity().getId() : 0) + "  order by c.state asc,c.name asc")) {
                                %>
                                <option value="<%=c.getId()%>"  <%=(Company.getCity() != null && Company.getCity().getId() == c.getId() ? "selected='true'" : "")%>><%=c.getName()%></option>
                                <%
                                            }
                                %>
                            </select>
                            <b style="color:red">*</b> <b id="validate4"></b>
                        </td>
                    </tr>
                    <tr>
                        <td class="label_bg">&nbsp;&nbsp;Logo:</td>
                        <td>&nbsp;&nbsp;<input type="file" name="fileLogo" accept="image/jpeg; image/gif; image/bmp; image/png;" value="Enviar" size="50px"></td>
                    </tr>
                    <tr>
                        <td class="label" valign="top">&nbsp;&nbsp;Descrição:</td>
                        <td>&nbsp;&nbsp;<textarea name="txtDescription" rows="5" cols="35" style="width:440px">
                                <%=Company.getDescription() == null ? "" : Company.getDescription()%>
                            </textarea>
                        </td>
                    </tr>
                </table>
                <table>
                    <tr>
                        <td class="label" align="right" colspan="2" width="600"><input type="button" onclick="javascript:verifyFields();" align="right" value="Gravar" name="enviar"></td>
                    </tr>
                </table>
                <input type="hidden" name="txtInsertDate" value="<%=Company.getInsertDate() == null ? formatBra.format(new java.util.Date()) : formatBra.format(Company.getInsertDate())%>">
                <input type="hidden" name="oldLogo" value="<%=Company.getLogo()%>">
                <input type="hidden" name="txtOldPassword" value="<%=Company.getPassword()%>"/>
            </form>
            <br>
            <table id="rodape" style="background-image:url(../../add/images/rodape.png);">
                <tr>
                </tr>
            </table>
            <table id="btRodape" style="text-decoration: none;" >
                <tr>
                    <td align="center" width="80px">
                        <ul class="btYoutube">
                            <li><a title="Youtube" href="http://www.youtube.com" /></li>
                        </ul>
                    </td >
                    <td align="center" width="80px">
                        <ul class="btOrkut">
                            <li><a title="Orkut" href="http://www.orkut.com" /></li>
                        </ul>
                    </td>
                    <td align="center" width="205px">
                        <ul class="btTwitter">
                            <li><a title="Twitter" href="http://twitter.com" /></li>
                        </ul>
                    </td>
                    <td align="center">
                        <a title="Trabalhe Conosco" class="text" href="../../Carrer.jsp" >Trabalhe Conosco</a>
                    </td>
                    <td align="center"  width="">
                        <a title="Mapa do Site" class="text" href="../../SiteMap.jsp">Mapa do Site</a>
                    </td>
                    <td align="center" width="">
                        <a title="Contatos" class="text" href="../../Contact.jsp">Contatos</a>
                    </td>
                </tr>
            </table>
        </div>
    </body>
</html>